﻿namespace SourceCodeRepository.Persistence.Tests.RepositoryTest
{
    using Infra.NHibernateUnitOfWork;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using NHibernate;
    using NHibernate.Criterion;
    using NHibernate.Tool.hbm2ddl;
    
    public class RepositoryBaseTest
    {
        [TestInitialize]
        public void SetUp()
        {
            UnitOfWork.Start(new TestConfigurationContext());
           
            new SchemaExport(UnitOfWork.Configuration).Execute(false, true, false, false);
        }

        /// <summary>
        /// Does the transactional flush.
        /// </summary>
        protected void DoTransactionalFlush()
        {
            UnitOfWork.Current.TransactionalFlush();
        }

        /// <summary>
        /// Gets the unique result from the DB using the UoW
        /// </summary>
        /// <typeparam name="T">The entity type</typeparam>
        /// <param name="expression">Expression</param>
        /// <returns></returns>
        protected T GetUniqueResultFromUnitOfWork<T>(ICriterion expression)
        {
            ICriteria crit = UnitOfWork.CurrentSession.CreateCriteria(typeof(T));
            return crit.Add(expression).UniqueResult<T>();
        }
    }
}
